package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.utils.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Scientific;
import com.example.entity.Student;
import com.example.exception.CustomException;
import com.example.mapper.ScientificMapper;
import com.example.mapper.StudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ScientificService {

    @Resource
    private ScientificMapper scientificMapper;

    // 添加科研成果
    public String add(Scientific scientific) {
        try {
            scientificMapper.insert(scientific);
        } catch (DuplicateKeyException e) {
            return "获奖名称重复！"; // 捕获主键重复异常，返回错误信息
        }
        return null; // 添加成功，返回null
    }

    // 根据ID更新科研成果
    public void updateById(Scientific scientific) {
        scientificMapper.updateById(scientific);
    }

    // 根据ID查询科研成果
    public Scientific getById(Integer id) {
        return scientificMapper.selectById(id);
    }

    // 根据ID删除科研成果
    public void deleteById(Integer id) {
        scientificMapper.deleteById(id);
    }

    // 分页查询科研成果列表
    public PageInfo<Scientific> selectPage(Integer pageNum, Integer pageSize, Scientific scientific) {
        PageHelper.startPage(pageNum, pageSize); // 设置分页信息
        List<Scientific> scientifics = scientificMapper.selectAll(scientific); // 查询科研成果列表
        return PageInfo.of(scientifics); // 返回分页信息
    }
}
